package com.ruoyi.alert.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue alarmQueue() {
        return new Queue("alarm.queue", true, false, false,
                new HashMap<String, Object>() {{
                    put("x-message-ttl", 60000);
                    put("x-dead-letter-exchange", "alarm.dead.letter.exchange");
                    put("x-dead-letter-routing-key", "alarm.dead.letter.routingkey");
                }});
    }

    // 添加交换器定义
    @Bean
    public DirectExchange alarmExchange() {
        return new DirectExchange("alarm.exchange");
    }

    // 绑定队列到交换器
    @Bean
    public Binding alarmBinding(Queue alarmQueue, DirectExchange alarmExchange) {
        return BindingBuilder.bind(alarmQueue)
                .to(alarmExchange)
                .with("alarm.routingkey");
    }
    @Bean
    public Queue deadLetterQueue() {
        return new Queue("alarm.dead.letter.queue");
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("alarm.dead.letter.exchange");
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with("alarm.dead.letter.routingkey");
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}